package com.qiangesoft.sms.tencentcloud.handler;

import com.qiangesoft.sms.tencentcloud.config.TencentcloudSmsProperties;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 腾讯云短信处理器
 *
 * @author qiangesoft
 * @date 2023-05-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TencentcloudSendHandler {

    private final SmsClient smsClient;

    private final TencentcloudSmsProperties tencentcloudSmsProperties;

    public boolean send(String[] phones, String[] params) {
        return this.send(phones, tencentcloudSmsProperties.getTemplateId(), params);
    }

    public boolean send(String[] phones, String templateId, String[] params) {
        // 填充请求参数
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppId(tencentcloudSmsProperties.getAppId());
        req.setSignName(tencentcloudSmsProperties.getSignName());
        req.setTemplateId(templateId);
        req.setTemplateParamSet(params);
        req.setPhoneNumberSet(phones);

        try {
            // 方法发起请求
            SendSmsResponse res = smsClient.SendSms(req);
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

}
